import Cookies from "js-cookie"

export default {
    namespaced: true,
    state: {
        // token: Cookies.get('token') || localStorage.getItem('token') || sessionStorage.getItem('token') || '',
        userInfo: JSON.parse(Cookies.get('userInfo') || localStorage.getItem('userInfo') || sessionStorage.getItem('userInfo') || '{}'),
    },
    mutations: {
        setUserInfo(state, { userInfo, remember }) {
            state.userInfo = userInfo
            // 本地存储token
            if (remember) {
                Cookies.set('userInfo', JSON.stringify(userInfo), { expires: 7, path: '/' })
            } else {
                sessionStorage.setItem('userInfo', JSON.stringify(userInfo))
            }
        },
        removeToken(state) {
            state.userInfo = {}
            // 本地移除token
            localStorage.removeItem('token')
            sessionStorage.removeItem('token')
            Cookies.remove('token')
        }
    },
    actions: {
        setToken({ commit }, token) {
            commit('setToken', token)
        }
    }
}